# Start log file for script -----------------------------------------------
TeachingDemos::txtStart("./log files/figure4_blackwhite_prep.txt")

# Load Data ---------------------------------------------------------
# individual data
load("./data/impdat1920.Race.WeekWeight.rdata")
load("./data/impdat20.Race.WeekWeight.rdata")

vars <- c("year", "week", "date_mdy", "ideo_mod", "ideo_con", "age_cat4", "edu_cat4", "race3", "sex",
          "census_region", "broughtwebsite2", "WeekWeights", "D_biep.White_Good_all")
impdat19_sub <- subset(impdat1920.WeekWeight, select = vars)
impdat20_sub <- subset(impdat20.WeekWeight, select = vars)

impdat_comb <- rbind(impdat19_sub, impdat20_sub)

# Race
impdat_comb$race_black <- ifelse(impdat_comb$race3 == "Black", 1, 0)
impdat_comb$race_black[which(is.na(impdat_comb$race3))] <- NA

# Models ----------------------------------
m_week1920_bw <- lm(D_biep.White_Good_all ~ 
                 as.factor(week)*year*race_black + ideo_mod + ideo_con +
                 age_cat4 + edu_cat4 + sex +
                 census_region + broughtwebsite2,
               data = impdat_comb, weights = WeekWeights,
               subset = (date_mdy != "2019-05-25" & date_mdy != "2020-05-25") & (race3 == "White" | race3 == "Black"))
summary(m_week1920_bw)

# Prediction Data Sets ------------------------------------------------
weeks19 <- names(table(impdat19_sub$week[which(!is.na(impdat19_sub$WeekWeights))]))
weeks20 <- names(table(impdat20_sub$week[which(!is.na(impdat20_sub$D_biep.White_Good_all))]))
pred_weeks <- weeks20[weeks20 %in% weeks19]

pdat_wht <- data.frame(week = pred_weeks,
                   year = c(rep(2019, length(pred_weeks)), rep(2020, length(pred_weeks))),
                   ideo_mod = 0,
                   ideo_con = 0,
                   race_white = 1,
                   race_black = 0,
                   age_cat4 = "18-29",
                   edu_cat4 = "Post-Grad",
                   sex = "f",
                   census_region = "South",
                   broughtwebsite2 = "Assignment school/work")

pdat_blk <- data.frame(week = pred_weeks,
                       year = c(rep(2019, length(pred_weeks)), rep(2020, length(pred_weeks))),
                       ideo_mod = 0,
                       ideo_con = 0,
                       race_white = 0,
                       race_black = 1,
                       age_cat4 = "18-29",
                       edu_cat4 = "Post-Grad",
                       sex = "f",
                       census_region = "South",
                       broughtwebsite2 = "Assignment school/work")


# Save DF for Plot -----------------------------------------------------------------
preds_wht <- predict(m_week1920_bw, pdat_wht, se.fit = T)
preds_blk <- predict(m_week1920_bw, pdat_blk, se.fit = T)

p_dat <- data.frame(year = c(rep("2019", length(pred_weeks)), rep("2020", length(pred_weeks))),
                    week = pred_weeks,
                    pred = c(preds_wht$fit,preds_blk$fit),
                    se = c(preds_wht$se.fit, preds_blk$se.fit),
                    race = c(rep("White", length(pred_weeks)*2), rep("Black", length(pred_weeks)*2)))
p_dat$week <- as.Date(paste(2020, p_dat$week, 1, sep="-"), "%Y-%U-%u")
p_dat$week[c(1,54,107,160)] <- "2020-01-01" # from 00 pred_weeks
p_dat$pred[which(p_dat$week == "2020-11-23" & p_dat$year == "2020")] <- NA
p_dat$se[which(p_dat$week == "2020-11-23" & p_dat$year == "2020")] <- NA
p_dat$pred[which(p_dat$week == "2020-11-30" & p_dat$year == "2020")] <- NA
p_dat$se[which(p_dat$week == "2020-11-30" & p_dat$year == "2020")] <- NA

p_dat_bw <- p_dat

# End log file for script -------------------------------------------------
TeachingDemos::txtStop()